Skip to content

Conversation

@swcollard
Copy link
Contributor

@swcollard swcollard commented Aug 27, 2025

Apollo MCP Server Metrics

This PR adds metrics to count and measure request duration throughout the MCP server

  • All metrics use the OpenTelemetry meter with scope apollo.mcp
  • Duration metrics are recorded in milliseconds
  • Metrics are recorded after operation completion to capture final success/error state
Metric Name Metric Type Attributes Description Location
apollo.mcp.operation.duration f64_histogram success (bool), operation.id (string), operation.type (string) Records the duration of GraphQL operations in milliseconds graphql.rs
apollo.mcp.operation.count u64_counter success (bool), operation.id (string), operation.type (string) Counts the number of GraphQL operations executed graphql.rs
apollo.mcp.tool.duration f64_histogram success (bool), tool_name (string) Records the duration of MCP tool calls in milliseconds server/states/running.rs
apollo.mcp.tool.count u64_counter success (bool), tool_name (string) Counts the number of MCP tool calls server/states/running.rs
apollo.mcp.initialize.count u64_counter None Counts server initialization events server/states/running.rs
apollo.mcp.list_tools.count u64_counter None Counts list tools requests server/states/running.rs
apollo.mcp.get_info.count u64_counter None Counts get info requests server/states/running.rs

Operation-Specific Attributes

  • operation.id: String containing the operation identifier (persisted query ID or operation name, defaults to "unknown") - Note this is a high cardinality risk as the execute tool can create its own operations
  • operation.type: String indicating the type of operation:
    • persisted_query for operations using persisted query IDs
    • operation for regular GraphQL operations

Tool-Specific Attributes

  • tool_name: String containing the name of the MCP tool being called (e.g., "introspect", "search", "explorer", "execute", "validate", or custom operation names)
Screenshot 2025-08-27 at 11 29 28 AM

@swcollard swcollard requested a review from a team as a code owner August 27, 2025 14:03
@swcollard swcollard requested review from Copilot and removed request for a team August 27, 2025 14:04
@github-actions
Copy link

Changeset file missing for PR

All changes should include an associated changeset file.
Please refer to README for more information on generating changesets.

@swcollard swcollard requested a review from a team August 27, 2025 14:04
@apollo-librarian
Copy link

apollo-librarian bot commented Aug 27, 2025

✅ Docs preview ready

The preview is ready to be viewed. View the preview

File Changes

0 new, 6 changed, 0 removed
* (developer-tools)/apollo-mcp-server/(latest)/index.mdx
* (developer-tools)/apollo-mcp-server/(latest)/install.mdx
* (developer-tools)/apollo-mcp-server/(latest)/limitations.mdx
* (developer-tools)/apollo-mcp-server/(latest)/quickstart.mdx
* (developer-tools)/apollo-mcp-server/(latest)/guides/auth.mdx
* (developer-tools)/apollo-mcp-server/(latest)/_sidebar.yaml

Build ID: 3140ca8b610261a18871af37
Build Logs: View logs

URL: https://www.apollographql.com/docs/deploy-preview/3140ca8b610261a18871af37

This comment was marked as outdated.

@swcollard swcollard requested a review from Copilot August 27, 2025 14:54
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements comprehensive OpenTelemetry metrics for the Apollo MCP Server to track operation counts and durations across GraphQL operations and MCP tool calls.

  • Adds duration and count metrics for GraphQL operations with success status and operation metadata
  • Implements metrics tracking for MCP tool calls including success/failure status and tool names
  • Adds simple count metrics for server initialization, list tools, and get info requests

Reviewed Changes

Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.

File Description
crates/apollo-mcp-server/src/server/states/running.rs Adds metrics collection for MCP tool calls, initialization, list tools, and get info operations
crates/apollo-mcp-server/src/graphql.rs Implements metrics tracking for GraphQL operations with duration and count measurements
crates/apollo-mcp-server/Cargo.toml Adds opentelemetry_sdk test dependency for metrics validation
.changesets/feat_otel_metrics.md Documents the new metrics feature

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

health_check.record_rejection();
}

let attributes = vec![
Copy link

Copilot AI Aug 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The attributes vector is created on every tool call. Consider creating the meter and histogram instruments once during initialization and reusing them to avoid repeated allocations.

Copilot uses AI. Check for mistakes.
/// Execute as a GraphQL operation using the endpoint and headers
#[tracing::instrument(skip(self))]
async fn execute(&self, request: Request<'_>) -> Result<CallToolResult, McpError> {
let meter = global::meter("apollo.mcp");
Copy link

Copilot AI Aug 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The meter and histogram instruments are created on every GraphQL operation. Consider creating these once during initialization and reusing them to avoid repeated allocations and improve performance.

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might warrant some merit, if we can manage it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added lazy loading into meter.rs and updated all references to call get_meter() to only initialize this once

.u64_counter("apollo.mcp.initialize.count")
.build()
.add(1, &[]);
// TODO: how to remove these?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This leftover TODO caught my eye. 😂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this was from #117
@nicholascioli Do you know what we'd need to clean this up?

Copy link
Contributor

@DaleSeo DaleSeo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks for addressing my feedback. 🙇

@swcollard swcollard merged commit efce0cd into feature/telemetry Aug 28, 2025
8 checks passed
@swcollard swcollard deleted the otel-metrics branch August 28, 2025 17:26
swcollard added a commit that referenced this pull request Sep 2, 2025
* Implement metrics for mcp tool and operation counts and durations

* Changeset

* Unit test attribute setting in graphql.rs

* Add axum_otel_metrics for emitting basic http metrics about requests

* Lazy load singleton Meter for metrics

* Alphabetize

* Simplify result.is_error checking
swcollard added a commit that referenced this pull request Sep 5, 2025
* Implement metrics for mcp tool and operation counts and durations

* Changeset

* Unit test attribute setting in graphql.rs

* Add axum_otel_metrics for emitting basic http metrics about requests

* Lazy load singleton Meter for metrics

* Alphabetize

* Simplify result.is_error checking
swcollard added a commit that referenced this pull request Sep 17, 2025
* Implement metrics for mcp tool and operation counts and durations

* Changeset

* Unit test attribute setting in graphql.rs

* Add axum_otel_metrics for emitting basic http metrics about requests

* Lazy load singleton Meter for metrics

* Alphabetize

* Simplify result.is_error checking
swcollard added a commit that referenced this pull request Sep 23, 2025
* Implement metrics for mcp tool and operation counts and durations

* Changeset

* Unit test attribute setting in graphql.rs

* Add axum_otel_metrics for emitting basic http metrics about requests

* Lazy load singleton Meter for metrics

* Alphabetize

* Simplify result.is_error checking
swcollard added a commit that referenced this pull request Sep 24, 2025
* Implement metrics for mcp tool and operation counts and durations

* Changeset

* Unit test attribute setting in graphql.rs

* Add axum_otel_metrics for emitting basic http metrics about requests

* Lazy load singleton Meter for metrics

* Alphabetize

* Simplify result.is_error checking
@apollo-bot2 apollo-bot2 mentioned this pull request Sep 24, 2025
@DaleSeo DaleSeo mentioned this pull request Sep 24, 2025
DaleSeo pushed a commit that referenced this pull request Sep 24, 2025
* Implement metrics for mcp tool and operation counts and durations

* Changeset

* Unit test attribute setting in graphql.rs

* Add axum_otel_metrics for emitting basic http metrics about requests

* Lazy load singleton Meter for metrics

* Alphabetize

* Simplify result.is_error checking
DaleSeo pushed a commit that referenced this pull request Sep 24, 2025
* Implement metrics for mcp tool and operation counts and durations

* Changeset

* Unit test attribute setting in graphql.rs

* Add axum_otel_metrics for emitting basic http metrics about requests

* Lazy load singleton Meter for metrics

* Alphabetize

* Simplify result.is_error checking
DaleSeo pushed a commit that referenced this pull request Sep 29, 2025
* Implement metrics for mcp tool and operation counts and durations

* Changeset

* Unit test attribute setting in graphql.rs

* Add axum_otel_metrics for emitting basic http metrics about requests

* Lazy load singleton Meter for metrics

* Alphabetize

* Simplify result.is_error checking
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants